package com.controller;

import com.model.Message;
import com.model.MessageDTO;
import com.model.Uinfo;
import com.model.UinfoFollow;
import com.response.Response;
import com.service.MessageService;
import com.service.UinfoFollowService;
import com.util.PageBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Resource
    private MessageService messageService;


    /**
     * 添加私信
     *
     * @param Message
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping(value = "/addMessage")
    @CrossOrigin
    public Response addMessage(@RequestBody Message message, HttpServletRequest request) throws Exception {
        try {
            messageService.addMessage(message);
        } catch (Exception e) {
            return Response.error();
        }
        return Response.success();
    }

    /**
     * 用户信息列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/messageList")
    @CrossOrigin
    public Response<List<MessageDTO>> messageList(@RequestBody Message message, @RequestParam Integer currentPage, HttpServletRequest request) throws Exception {
        int offset = (currentPage - 1) * PageBean.PAGE_IETM;
        int counts = 0;  //总记录数
        PageBean page = new PageBean(offset);
        //查询记录总数
        counts = messageService.getCount(message);
        //获取当前页记录
        List<MessageDTO> messageList = messageService.queryMessageByUid(message, page);
        int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM : counts / PageBean.PAGE_IETM + 1;
        return Response.success(messageList, counts, page_count);
    }

    /**
     * 查看跟某个人的多条私信
     */
    @PostMapping("/messageListByFromId")
    @CrossOrigin
    public Response<List<Message>> messageListByFromId(@RequestBody Message message, HttpServletRequest request) throws Exception {
        //获取当前页记录
        List<Message> messageList = messageService.queryMessageListByFromId(message);
        return Response.success(messageList);
    }


}
